<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false" width="50%">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm"
        :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-col :span="12">
          <!-- 字段名称 -->
          <el-form-item label="字段名称" prop="columnName">
            <!--文本框-->
            <el-input v-model="dataForm.columnName" placeholder="字段名称" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 字段注释 -->
          <el-form-item label="字段注释" prop="columnComment">
            <!--文本框-->
            <el-input v-model="dataForm.columnComment" placeholder="字段注释" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 默认值 -->
          <el-form-item label="数据库类型" prop="dbType">
            <el-select v-model="dataForm.dbType" placeholder="请选择数据库类型" @change="dbTypeChangeHandle(dataForm.dbType)"
              clearable style="width: 100%;">
              <el-option v-for="template in this.$getDictList('DATABASE_TYPE', false)" :key="template.val"
                :label="template.label" :value="template.val"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 字段类型 -->
          <el-form-item label="业务类型" prop="columnTypeBiz">
            <el-select v-model="dataForm.columnTypeBiz" filterable placeholder="字段类型"
              @change="columnTypeChangeHandle(dataForm.columnTypeBiz)" clearable style="width:100%">
              <el-option v-for="item in columnTypeList" :key="item.id" :label="item.columnTypeBiz" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 长度 -->
          <el-form-item label="长度" prop="maxLength">
            <el-input v-model="dataForm.maxLength" placeholder="长度" :disabled="dataForm.limitFlag" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 小数位数 -->
          <el-form-item label="小数位数" prop="numericScale">
            <el-input v-model="dataForm.numericScale" placeholder="小数位数" :disabled="dataForm.limitFlag" clearable>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 是否为空 -->
          <el-form-item label="可为空?" prop="isNullable">
            <el-checkbox v-model="dataForm.isNullable"></el-checkbox>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 排序 -->
          <el-form-item label="排序" prop="sort">
            <!--计数器-->
            <el-input-number v-model="dataForm.sort" :min="1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 默认值 -->
          <el-form-item label="默认值" prop="columnDefault">
            <!--文本框-->
            <el-input v-model="dataForm.columnDefault" placeholder="默认值" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 备注说明 -->
          <el-form-item label="备注说明" prop="remark">
            <!--文本域-->
            <el-input type="textarea" :rows="3" v-model="dataForm.remark" placeholder="备注说明"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      columnTypeList: [],
      dataForm: {
        id: '',
        columnName: '',
        columnComment: '',
        columnType: '',
        columnTypeId: '',
        columnTypeBiz: '',
        limitFlag: '',
        isNullable: '',
        columnDefault: '',
        maxLength: '',
        numericScale: '',
        dbType: '',
        sort: '',
        remark: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        columnName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        columnComment: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        columnTypeBiz: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        sort: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ],
        dbType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }

        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        } else {
          this.getSortInfo()
        }
      })
    },
    // 数据库类型改变事件
    dbTypeChangeHandle (dbType) {
      this.getColumnTypeList(dbType)
    },
    // 字段类型改变事件
    columnTypeChangeHandle (id) {
      let obj = {}
      obj = this.columnTypeList.find((item) => {
        return item.id === id
      })
      this.dataForm.maxLength = obj.maxLength
      this.dataForm.numericScale = obj.numericScale
      this.dataForm.columnType = obj.columnType
      this.dataForm.columnTypeId = obj.id
      this.dataForm.limitFlag = obj.limitFlag
      this.dataForm.columnTypeBiz = obj.columnTypeBiz
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/dpd/dpdColumn/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        // 重新加载字典类型
        this.getColumnTypeList(this.dataForm.dbType)
      }).catch(() => { })
    },
    // 获取排序
    getSortInfo () {
      this.$http.get(`/dpd/dpdColumn/getMaxSort`).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.dataForm.sort = res.data.sort
      }).catch(() => { })
    },
    // 获取字段类型
    getColumnTypeList (dbType) {
      this.$http.get(`/dpd/dpdColumnType/list`, { params: { dbType: dbType } }).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.columnTypeList = res.data
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/dpd/dpdColumn/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
